#!/bin/bash

# =============================================
# 1. Git Submodule Initialization & Update
# =============================================
# cd <main_repo_dir>  # Uncomment and set your main repo path if needed, e.g., cd /path/to/repo
git submodule init
git submodule update

# =============================================
# 2. Copy mcp_center to Target System Directory (if not exists)
# =============================================
TARGET_DIR="/usr/lib/euler-copilot-framework/mcp_center"
if [ -d "$TARGET_DIR" ]; then
    echo "mcp_center already exists, skipping copy: $TARGET_DIR"
else
    mkdir -p "/usr/lib/euler-copilot-framework"
    cp -rf ./mcp_center "$TARGET_DIR"
    echo "Creating and copying: ./mcp_center -> $TARGET_DIR"
fi

# =============================================
# 3. Check Python Version >= 3.11
# =============================================
REQUIRED_PYTHON="3.11"
PYTHON_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
echo "Current Python version: $PYTHON_VERSION"
echo "Required Python version >= $REQUIRED_PYTHON"

if [[ "$(printf '%s\n%s' "$PYTHON_VERSION" "$REQUIRED_PYTHON" | sort -V | head -n1)" != "$REQUIRED_PYTHON" ]]; then
    echo "Installation error: Current Python version $PYTHON_VERSION is lower than required version $REQUIRED_PYTHON"
    echo "Please use Python 3.11 or a higher version.\n"
    exit 1
fi

echo "Python version check passed, continuing with installation..."

# =============================================
# 4. Install Python Package (setup.py)
# =============================================
pip install .
mkdir -p /etc/sysintd/config
cp -rf config/* /etc/sysintd/config/

# =============================================
# 5. Check and Kill Existing sysintd Processes
# =============================================
echo "Checking for running sysintd processes..."

# Find all PIDs of processes containing "sysintd" in the command line
PIDS=$(pgrep -f "sysintd")

if [ -n "$PIDS" ]; then
    echo "Found existing sysintd processes (PIDs: $PIDS), attempting to terminate them..."
    pkill -f "sysintd"
    # Optional: wait a moment to ensure processes are terminated
    sleep 1
    # Double-check if any are still running
    REMAINING_PIDS=$(pgrep -f "sysintd")
    if [ -n "$REMAINING_PIDS" ]; then
        echo "Warning: Some sysintd processes may not have been terminated successfully (PIDs: $REMAINING_PIDS)"
    else
        echo "Successfully terminated existing sysintd processes"
    fi
else
    echo "No running sysintd processes found"
fi

# =============================================
# 6. Start sysintd as a Background Process (nohup)
# =============================================
# If sysint_ctl is meant to start the service instead, you could replace it with:
# sudo systemctl start sysintd
echo "Starting sysintd service..."
nohup sysintd > run_sysintd.log 2>&1 &

# Optional: Print log file location
echo "sysintd log output: $(pwd)/run_sysintd.log"

# =============================================
# 7. Run sysint_ctl -h (e.g., for help or initialization)
# =============================================
echo "Running sysint_ctl -h..."
sysint_ctl -h